<style scoped>
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }

  .weui-loadmore_line .weui-loadmore__tips {
    background-color: #eee;
  }

  .rj-cell.max span {
    font-size: 16px;
    padding-right: 20px;
  }

  .rj-cell label {
    color: #333;
    font-size: 14px;
    width: 44%;
    display: inline-block;
    padding: 5px 3%;
    text-align: right;
  }

  .rj-cell span {
    font-size: 14px;
    color: #333;
    display: inline-block;
    width: 50%;
  }

  .rj-cell.note label {
    float: left;
  }

  .rj-cell.note span {
    width: 80%;
    display: inline-block;
  }

  .weui-cells {
    margin-top: 0;
  }

  .weui-cells_form {
    background: #fbfbfb;
  }

  .weui-cell {
    background: #fff;
  }

  .weui-cells__title {
    text-align: left;
  }

  .oitem {
    padding: 3px;
  }

  .hide {
    display: none;
  }

  #acts {
    font-size: 0;
  }

  #acts span {
    font-size: 14px;
  }

  .halfbtn {
    display: inline-block;
    width: 49%;
  }

  .weui-label {
    text-align: left;
  }

  .time {
    border: none;
    font-size: 17px;
  }

  .weui-cell_select {
    padding: 10px 15px;
  }

  .weui-select {
    height: auto;
    line-height: normal;
  }


  .weui_uploader_input_wrp {
    float: left;
    position: relative;
    margin-right: 9px;
    margin-bottom: 9px;
    width: 77px;
    height: 77px;
    border: 1px solid #D9D9D9;
  }

  .weui_uploader_hd {
    padding-top: 0;
    padding-right: 0;
    padding-left: 0;
  }

  .weui_uploader_files {
    list-style: none;
  }

  .weui_uploader_input_wrp:before {
    width: 2px;
    height: 39.5px;
  }

  .weui_uploader_input_wrp:before, .weui_uploader_input_wrp:after {
    content: " ";
    position: absolute;
    top: 50%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    background-color: #D9D9D9;
  }

  .weui_uploader_input {
    position: absolute;
    z-index: 1;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  }

  .weui_uploader_input_wrp:after {
    width: 39.5px;
    height: 2px;
  }

  .weui_uploader_bd {
    overflow: hidden;
  }

  .weui_cell {
    padding: 10px 15px;
    position: relative;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
  }

  .outstore >>> .weui-uploader_files {
    list-style: none;
    overflow-y: hidden;
  }

  .outstore >>> .weui-uploader_file {
    float: left;
    margin-right: 9px;
    margin-bottom: 9px;
    width: 79px;
    background: no-repeat 50%;
    background-size: cover;
  }

  .weui-cell.fir {
    border-top: 0;
  }

  .help {
    text-align: right;
    padding: 5px;
    font-size: 12px;
    color: #bbb;
  }

  .weui_uploader_input_wrp.inrow {
    width: 30px;
    height: 30px;
    margin: 0;
  }

  .weui_uploader_input_wrp:before {
    height: 19.5px;
  }

  .weui_uploader_input_wrp:after {
    width: 19.5px;
  }

  .weui-uploader_file.inrow {
    margin: 0;
    width: 40px;
  }

  li.weui-uploader_file {
    display: inline-block;
    padding: 3px 5px;
  }

  ul#images {
    text-align: right;
  }

  .addPur >>> .ui.selection.dropdown {
    border: none;
  }
  .addPur >>> .ui.dropdown .menu>.item {
    font-size: 18px;
  }
  .addPur >>> .ui.search.dropdown>.text {
    font-size: 18px;
  }

  /*.addPur >>> .ui.search.dropdown>.text {*/
  /*  line-height: 20px;*/
  /*}*/
</style>
<template>
  <div class="addPur" style="overflow: hidden;">
    <div
      style="height: 50px; line-height: 50px; background: #f7f7f7; text-align: center; font-size: 16px; border-bottom: 1px solid #bdbbbc; position: relative">
      编辑销售订单
      <a href="javascript:;" @click="$router.back(-1)" style="position: absolute; left: 10px; top: 8px">
        <img src="@/assets/back.png" style="width: 20px"/>
      </a>
    </div>
    <div class="weui-cells weui-cells_form">
      <form method="post" id="formid">
        <input type="hidden" name="id" :value="baseInfo.id" />
        <input type="hidden" name="guid" :value="baseInfo.guid" />
        <input type="hidden" name="arriProvince" />
        <input type="hidden" name="arriCity"/>
        <input type="hidden" name="arriDistrict"/>
        <input type="hidden" name="type" :value="currCustomer.flag == 3 ? 2 : 1"/>
        <input v-if="currDepart" type="hidden" name="sorgguid" v-model="currDepart.orgguid"/>
        <input v-if="currDepart" type="hidden" name="porgname" v-model="currDepart.orgname"/>
        <input type="hidden" name="partitionflag" :value="partitionflag"/>
        <div class="cellgroup">
          <div class="weui-cell weui-cell_select">
            <div class="weui-cell__hd"><label class="weui-label" ref="msgDiv" style="width: 66px;">申请人</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input notnull" type="text" name="applyname" readonly="true" v-model="username"
                     style="width: 25%;">
              <input v-if="currDepart" type="hidden" name="depname" ref="depname" v-model="currDepart.name"/>
              <select class="weui-select" v-model="departid" id="depart" style="width: 70%;padding-left: 5px;padding-right: 5px;">
                <option value="">请选择部门</option>
                <option selected="" v-for="(item, index) in departments" :key="item.id" :value="item.id">
                  {{item.name}}
                </option>
              </select>
            </div>
          </div>
          <div class="weui-cell" v-if="flowid == 36">
            <div class="weui-cell__hd"><label class="weui-label">订单类型</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="flag" v-model="flag">
                <option v-for="(item, index) in sellformFlags" :value="item.dictValue" >{{item.dictLabel}}</option>
              </select>
            </div>
          </div>
          <input v-if="flowid != 36" type="hidden" name="flag" :value="flag" />
        </div>
        <div class="cellgroup">
          <div class="weui-cell fir">
            <div class="weui-cell__hd"><label class="weui-label">客户</label></div>
            <div class="weui-cell__bd">
              <input type="hidden" name="customerguid" v-model="currCustomer.guid"/>
              <input type="hidden" name="customername" ref="customername" v-model="currCustomer.name"/>
              <model-list-select :list="customers"
                                 v-model="customerid"
                                 option-value="guid"
                                 option-text="name"
                                 placeholder="请选择" style="border: 0;">
              </model-list-select>
            </div>
          </div>
          <div class="weui-cell weui-cell_select">
            <div class="weui-cell__hd"><label class="weui-label">收货城市</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="endCity" name="" placeholder="请选择收货城市">
            </div>
            <div class="weui-cell__ft" v-if="hasCustomer">
              <div class="weui-vcode-btn" @click="manageAddr"><i class="weui-icon-info-circle"></i></div>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">详细地址</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="arriAddress" name="arriAddress" placeholder="请输入收货详细地址">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">收货联系人</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="arriContact" name="arriContact" placeholder="请输入收货联系人">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">联系电话</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id="arriContactNo" name="arriContactNo" placeholder="请输入收货人电话">
            </div>
          </div>
        </div>
        <div class="cellgroup">
          <div class="weui-cell fir">
            <div class="weui-cell__hd">
              <label class="weui-label">发货日期</label>
            </div>
            <div class="weui-cell__bd">
              <input type="text" id='datetime-picker' class="time" name="" placeholder="请选择要求发货日期" autocomplete="off"/>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd">
              <label class="weui-label">到货日期</label>
            </div>
            <div class="weui-cell__bd">
              <input type="text" id='datetime-picker2' class="time" name="" placeholder="请选择要求到货日期"/>
            </div>
          </div>
          <!--          <div class="weui-cell weui-cell_select" v-if="flowid == 36">-->
          <!--            <div class="weui-cell__hd"><label class="weui-label">物流结算</label></div>-->
          <!--            <div class="weui-cell__bd">-->
          <!--              <select class="weui-select" name="faretype" v-model="faretypekey">-->
          <!--                <option value="" disabled>请选择</option>-->
          <!--                <option selected="" v-for="(item, index) in faretypes" :key="item.dictValue" :value="item.dictValue">-->
          <!--                  {{item.dictLabel}}-->
          <!--                </option>-->
          <!--              </select>-->
          <!--            </div>-->
          <!--          </div>-->
          <div class="weui-cell weui-cell_select" v-if="flowid == 36">
            <div class="weui-cell__hd"><label class="weui-label">配送方式</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="deliverytype" v-model="deliverytype">
                <option value="" disabled>请选择</option>
                <option v-for="(item, index) in deliverytypes" :value="item.dictValue">{{item.dictLabel}}</option>
              </select>
            </div>
          </div>
          <input v-if="flowid != 36" type="hidden" name="deliverytype" :value="deliverytype" />
          <div class="weui-cell weui-cell_select">
            <div class="weui-cell__hd"><label class="weui-label">结算币种</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="currency" v-model="currency">
                <option value="" disabled>请选择</option>
                <option selected="" v-for="(item, index) in currencys" :key="item.dictValue" :value="item.dictValue">
                  {{item.dictLabel}}
                </option>
              </select>
            </div>
          </div>
          <div class="weui-cell weui-cell_select">
            <div class="weui-cell__hd"><label class="weui-label">付款方式</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="paytype" v-model="paytype">
                <option value="" disabled>请选择</option>
                <option selected="" v-for="(item, index) in paytypes" :key="item.dictValue" :value="item.dictValue">
                  {{item.dictLabel}}
                </option>
              </select>
            </div>
          </div>
        </div>
        <div class="weui-cell fir">
          <div class="weui-cell__hd"><label class="weui-label">附件上传</label></div>
          <div class="weui-cell__ft">
            <div class="weui_uploader_bd">
              <ul class="weui_uploader_files"></ul>
              <div class="weui_uploader_input_wrp inrow">
                <UploadFile3 class="weui_uploader_input" v-on:pushfile="setFile"></UploadFile3>
              </div>
            </div>
          </div>
          <ul class="weui-uploader_files" id="images">
            <li class="weui-uploader_file" v-for="(fil, index) in filenames" @click=delFile(fil,fileids[index])>
              {{fil}}
            </li>
          </ul>
        </div>
        <div class="cellgroup">
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">备注</label></div>
            <div class="weui-cell__bd">
              <textarea class="weui-textarea" name="remarks" v-model="remark" placeholder="请输备注内容" rows="2"></textarea>
            </div>
          </div>
        </div>
        <div class="cellgroup">
          <div class="weui-cell fir">
            <div class="weui-cell__hd"><label class="weui-label">货品类型</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="quitsrtype" v-model="quitsrtype">
                <option v-for="(item, index) in productTypes" :value="item.dictValue" >{{item.dictLabel}}</option>
              </select>
            </div>
          </div>
          <div class="weui-cell weui-cell_select">
            <div class="weui-cell__hd"><label class="weui-label">计价方式</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="settletype" v-model="settletype">
                <option value="" disabled>请选择</option>
                <option selected="" v-for="(item, index) in settletypes" :key="item.dictValue" :value="item.dictValue">
                  {{item.dictLabel}}
                </option>
              </select>
            </div>
          </div>
        </div>
        <EditSellformItem v-for="(item, index) in listdata.oitems" :key="index"  :initItemlen="allItems.length" :itemNo="index" :itemdata="item" :isLast="index+1 == listdata.oitems.length"
                          :products="products" :departid="departid"
                          :quitsrtype="quitsrtype" :settletype="settletype" :paytype="paytype"
                          :prodpricestra="prodpricestra" :currency="currency" :orgid="currDepart ? currDepart.orgId : ''" @doAddItem="addItem(index + 1)"
                          @doDelItem="delItem" @changeData="takeItem"></EditSellformItem>
        <div class="weui-btn-area">
          <a class="weui-btn weui-btn_primary subbtn" href="javascript:" @click="postForm(2)">提交</a>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
  import 'vue-search-select/dist/VueSearchSelect.css'
  import { ModelListSelect } from 'vue-search-select'
  import EditSellformItem from "./EditSellformItem";
  import UploadFile3 from "../common/UploadFile3";
  import qs from 'qs';

  export default {
    name: 'AddSellform2',
    data() {
      return {
        username: localStorage.username,
        customers: [],
        currCustomer: '',
        customerid: '',
        departments: [],
        currDepart: '',
        departid: '',
        fpricetype: 1,
        faretypes: [], //物流结算方式（公司结算、客户结算）
        faretypekey: '',
        customerguid: '',
        customername: '',
        products: [],
        saleSpec: [],
        oitems: [0],
        quitsrtype: '',
        currStroom: '',
        settletypes: [],
        settletype: '',
        paytypes: [], //付款方式（全额预付、上车付款、到货付款）
        paytype: '',
        flag: 1,
        deliverytypes: [],
        deliverytype: '',
        discount: '',
        currencys: [], //结算币种
        currency: 1,
        prodpricestra: 2,  //为1获取不到价格不许提交，2.允许手动录入,
        senddate: '',
        arrivedate: '',
        filenames: [],
        fileids: [],
        artoken: '',
        productTypes: [],
        model: {
          items: []
        },
        baseInfo: {},
        allItems: '',
        sellitems: [],
        listdata: {},
        remark: '',
        partitionflag: '',
        title: '',
        nodeid: '',
        annex: '',
        flowid: 34,
        sellformFlags: [],
      }
    },
    components: {ModelListSelect, EditSellformItem, UploadFile3},
    computed: {
      pageTitle: function () {
        let tit = '销售订单';
        switch (this.flowid) {
          case '34':
            tit = '电商订单';
            break;
          case '35':
            tit = '样品订单';
            break;
          default:
            tit = '销售订单';
            break;
        }
        return tit;
      }
    },
    created() {
      if (this.$route.query.flowid) this.flowid = this.$route.query.flowid;
      //页面title设置
      document.title = '土豆集-' + this.pageTitle;
    },
    async mounted() {
      let that = this;
      //日历初始化
      $("#datetime-picker").calendar();
      $("#datetime-picker2").calendar();

      $("#datetime-picker").change(function () {
        that.senddate = $(this).val().replace(/\//g, '-');
      })
      $("#datetime-picker2").change(function () {
        that.arrivedate = $(this).val().replace(/\//g, '-');
      })

      //地址初始化
      $("#endCity").cityPicker();
      $("#endCity").change(function () {
        console.log('触发了endCity')
        let addr = $(this).val().split(' ');
        $('input[name="arriProvince"]').val(addr[0]);
        $('input[name="arriCity"]').val(addr[1]);
        $('input[name="arriDistrict"]').val(addr[2]);
      })

      this.id = this.$route.query.id;
      this.nodeid = this.$route.query.nodeid;
      this.annex = this.$route.query.annex;
      this.title = this.$route.query.title;

      //显示加载状态
      $.showLoading("正在加载");
      //获取select下拉数据
      this.getFaretype();
      this.getProducttypes();
      this.getSettletypes();
      this.getDeliverytypes();
      this.getCurrencys();
      this.getPaytypes();
      this.getSellformFlags();
      await this.getDepartment();
      this.getBaseInfo(this.id);
    },
    watch: {
      'departid': function (data) {
        this.currDepart = this.departments.find(function (item) {
          return item.id == data;
        });
        if (this.currDepart.guid == this.baseInfo.depguid) return;
        if (this.currDepart) {
          this.getCustomer(this.currDepart.guid);
        }
      },
      'customerid': function (data) {
        console.log('触发了customerid')
        console.log('this.customers:', this.customers)
        if (!this.customers || this.customers.length <= 0) return;
        this.currCustomer = this.customers.filter(function (item) {
          return item.guid == data;
        })[0] || '';
        this.currency = this.currCustomer.currency ? this.currCustomer.currency : 1;
        this.$store.commit('setCustomer', this.currCustomer);
      },
      // 'quitsrtype': function (val) {
      //   if (val == 2) {
      //     this.fpricetype = 1;
      //     this.settletype = 1;
      //     this.deliverytype = 2;
      //   } else if (val == 1 || val == 3) {
      //     this.fpricetype = 2;
      //     this.settletype = 2;
      //     this.deliverytype = 1;
      //   }
      // },
      'deliverytype': function (val) {
        this.faretypekey = val == 1 ? 2 : 1;
      },
      currCustomer: function (currCustomer) {
        if (currCustomer.province) {
          $('#endCity').val(currCustomer.province + ' ' + currCustomer.city + ' ' + currCustomer.district);
          $('input[name="arriProvince"]').val(currCustomer.province);
          $('input[name="arriCity"]').val(currCustomer.city);
          $('input[name="arriDistrict"]').val(currCustomer.district);
        }

        $('#arriAddress').val(currCustomer.address);
        $('#arriContact').val(currCustomer.contact);
        $('#arriContactNo').val(currCustomer.contactno);
      }
    },
    methods: {
      takeQuitsrtype: function(val) {
        if (val == 2) {
          this.fpricetype = 1;
          this.settletype = 1;
          this.deliverytype = 2;
        } else if (val == 1 || val == 3) {
          this.fpricetype = 2;
          this.settletype = 2;
          this.deliverytype = 1;
        }
      },
      getBaseInfo: function (id) {
        this.axios({
          method: 'get',
          url: `/prod-api/bill/sellform//detail`,
          params: {orderid: id}
        }).then(async response => {
          if (response.data.code == 200) {
            let data = response.data.data;
            this.baseInfo = data.result || {};
            this.sellitems = this.baseInfo.items;
            this.allItems = this.baseInfo.spitems ? this.baseInfo.spitems.concat(this.baseInfo.items) : this.baseInfo.items;

            if (this.departments.length > 0) {
              this.departid = this.baseInfo.deptId;
            }
            if (this.customers.length <= 0) {
              await this.getCustomer(this.baseInfo.depguid);
            }
            this.customerid = this.baseInfo.customerguid;
            this.quitsrtype = this.baseInfo.quitsrtype;
            this.flag = this.baseInfo.flag;
            this.partitionflag = this.baseInfo.partitionflag;
            this.deliverytype = this.baseInfo.deliverytype;
            this.settletype = this.baseInfo.settletype;
            this.currency = this.baseInfo.currency;
            this.paytype = this.baseInfo.paytype;
            this.remark = this.baseInfo.remarks;
            $('#endCity').val(this.baseInfo.arriProvince + ' ' + this.baseInfo.arriCity + ' ' + this.baseInfo.arriDistrict);
            $('input[name="arriProvince"]').val(this.baseInfo.arriProvince);
            $('input[name="arriCity"]').val(this.baseInfo.arriCity);
            $('input[name="arriDistrict"]').val(this.baseInfo.arriDistrict);
            $('#arriAddress').val(this.baseInfo.arriAddress);
            $('#arriContact').val(this.baseInfo.arriContact);
            $('#arriContactNo').val(this.baseInfo.arriContactNo);
            if (this.baseInfo.senddate != null) {
              $("#datetime-picker").val(this.baseInfo.senddate.replace(/-/g, '/'));
              this.senddate = this.baseInfo.senddate.replace(/-/g, '/');
            }
            if (this.baseInfo.arrivedate != null) {
              $("#datetime-picker2").val(this.baseInfo.arrivedate.replace(/-/g, '/'));
              this.arrivedate = this.baseInfo.arrivedate.replace(/-/g, '/');
            }
            //明细
            this.$set(this.listdata, 'oitems', this.allItems);
            console.log('this.listdata.oitems:', this.listdata.oitems)
            console.log('this.listdata.oitems.length:', this.listdata.oitems.length)
            this.$forceUpdate();
          } else if (response != undefined && response.data != undefined && response.data.msg == 'sessionTimeOut') {
            //不不设置提示
          } else {
            this.toastError('系统错误');
          }
          //关闭加载状态
          $.hideLoading();
          this.isReady = true;
        });
      },
      getProducttypes: async function() {
        let that = this;
        this.axios({
          method: 'get',
          url: '/prod-api/system/dict/data/type/product_type',
        }).then(response => {
          if (response.data.code == 200) {
            that.productTypes = response.data.data.filter(item => item.dictValue != 4);
          } else {
            this.toastText('获取货品类型数据失败');
          }
        });
      },
      takeItem: function(val) {
        console.log('takeItem:', val)
        if (this.model.items.some(item => {
          return item.index == val.index;
        })) {
          this.model.items = this.model.items.map(item => {
            if (item.index == val.index) {
              return val;
            }
            return item;
          })
        } else {
          this.model.items.push(val);
        }
        console.log('this.model.items:', this.model.items)
      },
      addItem: function(index) {
        this.listdata.oitems.push(index);
      },
      delItem: function() {
        this.listdata.oitems.pop();
        this.model.items.pop();
      },
      delFile: function (name, id) {
        let that = this;
        $.confirm('确定要删除？', function () {
          that.axios({
            method: 'post',
            // url: '/sales/Fileinfo!delete.action',
            // params: {fileid: id}
            url: '/prod-api/common/delete',
            params: {id: id}
          })
          that.fileids = that.fileids.filter(item => {
            return item != id;
          })
          that.filenames = that.filenames.filter(item => {
            return item != name;
          })
        })
      },
      setFile: function (file) {
        this.filenames.push(file.name);
        this.fileids.push(file.id);
      },
      setCustomer: function () {
        this.currCustomer = this.$store.state.customer;
      },
      manageAddr: function () {
        this.$router.push({name: 'AddressList', query: {flowid: this.flowid}});
        return false;
      },
      getStateAddr: function () {
        let addr = this.$store.state.arriAddr;

        $('input[name="arriProvince"]').val(addr.province);
        $('input[name="arriCity"]').val(addr.city);
        $('input[name="arriDistrict"]').val(addr.district);
        $('#endCity').val(addr.province + ' ' + addr.city + ' ' + addr.district);
        $('#arriAddress').val(addr.address);
        $('#arriContact').val(addr.contact);
        $('#arriContactNo').val(addr.contactno);
      },
      //客户
      getCustomer: async function (departid) {
        let that = this;
        if (!departid) return;
        await this.axios({
          method: 'get',
          url: '/prod-api/customer/customer/getCustomers',
          params: {departId: departid, functionType: 13}
        }).then(response => {
          if (response.data.code == 200) {
            that.customers = response.data.data;
            // that.customerid = that.customers[0].id;
          } else {
            this.toastText('获取客户数据失败');
          }
        });
      },
      //部门
      filterDepartment: function (item) {
        return {title: item.name, value: item.guid};
      },
      getDepartment: async function () {
        let that = this;
        await this.axios({
          method: 'get',
          url: '/prod-api/organization/department/findDepartment',
          params: {permission: 2}
        }).then(response => {
          if (response.data.code == 200) {
            that.departments = response.data.data;
          } else if (response.data.msg) {
            this.toastText(response.data.msg);
          }  else {
            this.toastText('获取部门数据失败');
          }
        });
      },
      getSellformFlags: function() {
        let that = this;
        this.axios({
          method: 'get',
          url: '/prod-api/system/dict/data/type/sellform_flag',
        }).then(response => {
          if (response.data.code == 200) {
            that.sellformFlags = response.data.data.filter(item => item.dictValue != 4);
          } else {
            this.toastText('获取订单类型数据失败');
          }
        });
      },
      //物流结算
      getFaretype: function () {
        let that = this;
        this.axios({
          method: 'get',
          url: '/prod-api/system/dict/data/type/wlinfo_fare_type',
        }).then(response => {
          if (response.data.code == 200) {
            that.faretypes = response.data.data;
            this.faretypekey = response.data.data[0].dictValue;
          } else {
            this.toastText('获取物流结算方式数据失败');
          }
        });
      },
      getSettletypes: function () {
        let that = this;
        this.axios({
          method: 'get',
          url: '/prod-api/system/dict/data/type/sellform_pricetype',
        }).then(response => {
          if (response.data.code == 200) {
            that.settletypes = response.data.data;
            this.settletype = response.data.data[0].dictValue;
          } else {
            this.toastText('获取结算方式数据失败');
          }
        });
      },
      getDeliverytypes: function () {
        let that = this;
        this.axios({
          method: 'get',
          url: '/prod-api/system/dict/data/type/sellform_deliverytype',
        }).then(response => {
          if (response.data.code == 200) {
            that.deliverytypes = response.data.data;
          } else {
            this.toastText('获取物流配送方式数据失败');
          }
        });
      },
      getCurrencys: function () {
        let that = this;
        this.axios({
          method: 'get',
          url: '/prod-api/system/dict/data/type/currency_type',
        }).then(response => {
          if (response.data.code == 200) {
            that.currencys = response.data.data;
            that.currency = response.data.data[0].dictValue;
          } else {
            this.toastText('获取结算币种数据失败');
          }
        });
      },
      getPaytypes: function () {
        let that = this;
        this.axios({
          method: 'get',
          responseType: 'json',
          url: '/prod-api/system/dict/data/type/pay_type',
        }).then(response => {
          if (response.data.code == 200) {
            that.paytypes = response.data.data.filter(item => {
              if (this.flowid == 34) {
                return item.dictValue == 3;
              } else if (this.flowid == 35) {
                return item.dictValue == 5;
              } else {
                return item.dictValue != 5;
              }
            });
          } else {
            this.toastText('获取付款方式数据失败');
          }
        });
      },
      //提交表单数据
      postForm: function () {
        let that = this;
        //表单校验
        if (!this.checkFormElm()) {
          return false;
        }
        //防止重复提交
        $('.subbtn').attr("disabled",true).css("pointer-events","none");
        //显示提交状态
        $.showLoading("正在提交");
        let postData = this.serialize2Obj($("#formid").serializeArray());
        postData.senddate = this.senddate;
        postData.arrivedate = this.arrivedate;
        postData.items = this.model.items;
        postData.depguid = this.currDepart.guid;
        this.axios({
          method: 'post',
          url: `/prod-api/bill/sellform/save?fileIds=${this.fileids.join(',')}&deptId=${this.departid}&taskid=${this.nodeid}`,
          data: postData
        }).then(response => {
          //恢复提交按钮可点击
          $('.subbtn').attr('disabled', false).css("pointer-events","auto");
          //关闭提交状态
          $.hideLoading();

          if (response.data.code == 200) {
            this.toastSuccess("保存成功");
            this.$router.push({name: 'SellformList2', query: {flowid: this.flowid}});
          } else if (response.data.msg) {
            this.toastText(response.data.msg);
          } else {
            this.toastError('系统错误');
          }
        }).catch(function (error) {
          //恢复提交按钮可点击
          $('.subbtn').attr('disabled', false).css("pointer-events","auto");
          //关闭提交状态
          $.hideLoading();
          that.toastError('系统错误');
        });
      },
      checkFormElm: function () {
        if (!this.departid) {
          this.toastText('请选择部门！');
          return false;
        } else if (!this.customerid) {
          this.toastText('请选择客户！');
          return false;
        } else if (this.senddate == '') {
          this.toastText('要求发货日期不能为空！');
          return false;
        } else if (this.arrivedate == '') {
          this.toastText('要求到货日期不能为空！');
          return false;
        } else if (!this.paytype) {
          this.toastText('请选择付款方式！');
          return false;
        } else if (!this.checkEmy('product')) {
          this.toastText('品种不能为空！');
          return false;
        } else if (!this.checkEmy('specif')) {
          this.toastText('请选择货品规格！');
          return false;
        } else if ([1,2,3].includes(parseInt(this.paytype)) && !this.checkEmy('price')) {
          this.toastText('货品单价不能为空！');
          return false;
        } else if ([4].includes(parseInt(this.paytype)) && !this.checkEmy2('price')) {
          this.toastText('货品单价不能为空！');
          return false;
        } else if (!this.checkEmy('weight')) {
          this.toastText('请输入货品重量！');
          return false;
        }
        return true;
      },
      //判断明细产品是否重复
      checkProduct: function () {
        let parr1 = [];
        let parr2 = [];
        let items = this.model.items;
        for (let i = 0; i < items.length; i++) {
          parr1.push({[items[i].productguid]: items[i].spguid});
          parr2.push({[items[i].productguid]: items[i].spguid});
        }
        //json数组去重
        for (var i = 0; i < parr2.length; i++) {
          for (var j = i + 1; j < parr2.length;) {
            if (Object.keys(parr2[i])[0] == Object.keys(parr2[j])[0] && Object.values(parr2[i])[0] == Object.values(parr2[j])[0]) {
              parr2.splice(j, 1);//去除重复的对象；
            } else {
              j++;
            }
          }
        }
        return parr1.length == parr2.length ? true : false;
      }
    }
  }
</script>
